/**
 * 
 */
package galacticthrone.map.render;

import galacticthrone.map.data.obs.Ob;

import java.util.HashMap;
import java.util.Map;

import javax.media.opengl.GL;

import org.apache.log4j.Logger;

// TODO Javadoc
/**
 * <br>
 *
 * @author Jaco van der Westhuizen
 */
abstract public class MapIcon
{
    private static final Logger logger = Logger.getLogger(MapIcon.class);
    private static int nextIndex = Integer.MIN_VALUE;
    private static final Map<Integer, MapIcon> lookup = new HashMap<Integer, MapIcon>();
    static final IconAtlas mapIconAtlas = new IconAtlas((short)128, (short)256);

    protected final Ob ob;
    protected final float size;

    protected final int index;
    
    private String iconName;
    private short icon;

    public MapIcon(Ob ob, float diameter, String iconName)
    {
        if (ob == null)
            throw new IllegalArgumentException("MapIcon must have an Ob target.");
        
        this.ob = ob;
        this.size = diameter / 2;
        this.index = nextIndex++;
        lookup.put(index, this);
        
        this.setIcon(iconName);
    }
    
    protected void setIcon(String fileName)
    {
    	iconName = fileName;
    	icon = -1;
    }
    
    public void renderDisplay(GL gl)
    {
        // TODO Zoom-dependent size and visibility
    	if (icon < 0)
    	{
    		logger.warn("Map Icon without image.");
    		return;
    	}

        final float posX = ob.getPosX();
        final float posY = ob.getPosY();

        final float[] coords = mapIconAtlas.coords[icon];

        gl.glColor4f(1, 1, 1, 1);
        gl.glTexCoord2fv(coords, 0);
        gl.glVertex2f(posX - size, posY - size);
        gl.glTexCoord2fv(coords, 2);
        gl.glVertex2f(posX - size, posY + size);
        gl.glTexCoord2fv(coords, 4);
        gl.glVertex2f(posX + size, posY + size);
        gl.glTexCoord2fv(coords, 6);
        gl.glVertex2f(posX + size, posY - size);
    }
    
    public void renderPicking(GL gl)
    {
        float posX = ob.getPosX();
        float posY = ob.getPosY();

        gl.glPushName(index);

        gl.glBegin(GL.GL_QUADS);
        gl.glVertex2f(posX - size, posY - size);
        gl.glVertex2f(posX - size, posY + size);
        gl.glVertex2f(posX + size, posY + size);
        gl.glVertex2f(posX + size, posY - size);
        gl.glEnd();

        gl.glPopName();
    }
    
    public float getX()
    {
        return ob.getPosX();
    }

    public float getY()
    {
        return ob.getPosY();
    }
    
    public float getSize()
    {
        return size;
    }
    
    public static MapIcon lookup(int index)
    {
        return lookup.get(index);
    }

    public void prepareToRender(GL gl)
    {
        if (icon < 0)
        	icon = mapIconAtlas.getId(gl, iconName);
    }

	/**
     * @return The <code>Ob</code> represented by this <code>VisOb</code>.
     */
    public Ob getOb()
    {
	    return ob;
    }

	/**
     * @param gl
     */
    abstract public void renderText(GL gl);
}
